package inputstream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * FileInputStream类:
 *  构造方法:
 *      public FileInputStream(String name) throws FileNotFoundException
 *         FileInputStream要求读取的文件在硬盘上必须存在,否则抛出FileNotFoundException
 *  成员方法:
 *      public int read() throws IOException
 *         从输入流关联的文件中读取一个字节
 *      public void close() throws IOException
 *         关闭此文件输入流关联的底层资源(cpu,硬盘)
 */
public class FileInputStreamDemo01 {
    public static void main(String[] args)throws Exception {
        //method01();
        FileInputStream fis = new FileInputStream("day22-IO-01/5.txt");
/*        int b1 = fis.read();
        System.out.println((char) b1);//97 => ACSII码 => a

        int b2 = fis.read();
        System.out.println((char) b2);//98 => ACSII码 => b

        int b3 = fis.read();
        System.out.println((char) b3);//99 => ACSII码 => c

        int b4 = fis.read();
        System.out.println(b4);//-1 文件中有一个隐含的文件结束标志EOF(End Of File)*/

        /**
         * 第一次循环 fis.read() (97=>a),循环体中再次fis.read()(98=>b),输出b
         * 第二次循环 fis.read() (99=>c),循环体中再次fis.read()(-1),对-1强转乱码
         * 第三次循环上的fis.read()结果返回-1 循环条件为false,不再执行循环
         */
        //错误写法
        /*while (fis.read() != -1) {
            System.out.println((char) fis.read());
        }*/

        int b;
        while ((b = fis.read()) != -1) {
            System.out.println((char) b);
        }

        fis.close();
    }

    private static void method01() throws FileNotFoundException {
        FileInputStream fis = new FileInputStream("day22-IO-01/5.txt");//java.io.FileNotFoundException: day22-IO-01\5.txt (系统找不到指定的文件。)
    }
}
